package com.ttx.springmvc.example2.config;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author TimFruit
 * @date 2019/7/30 9:21
 */

public class UserLoginInterceptor implements HandlerInterceptor {  //或者继承 HandlerInterceptorAdapter
    
    // 在执行之前调用 
    @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
        
        // 一般是获取cookie, 并校验
        System.out.println("=======================");
        System.out.println("拦截路径："+request.getRequestURI());
        
        response.addHeader("my-message","this is a message from user login interceptor");
        
        return true; // true, 能继续执行下一个调用
    }

    // 在调用处理器之后处理
    @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {

        
        
        // 需要在前置操作中才能添加响应 
//        response.addHeader("Access-Control-Allow-Origin","拦截器判断用户已登录");
    }

    // 在渲染之后(整个mvc调用过程完成之后)处理 
    @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
        Exception ex) throws Exception {

    }
}
